
import {getProductApprovalList, getProductById,productApproval } from '../../services/customer/Product';
import { getHscodeByCode} from '../../services/basedata/customsProductCode';
import Constants from '../../utils/Constants';

import { message } from "antd";
const R = require('ramda');

export default {
    namespace: 'productApproval',
    state: {
        productPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        productList:[],
        dataSource: {},
        product: {},
        hscode: {},
        attachments:{},
    },
    reducers: {

        //-----------------setList

        /**
         * 设置产品列表
         * @param state
         * @param payload
         * @returns {{productList, productPage: *}}
         */
        setProductList(state, { payload }) {
            return { ...state, productList: payload.productList, productPage: payload.productPage}
        },

        //--------------------setEntity


        /**
         * 设置产品
         * @param state
         * @param action
         * @returns {{product: (string|state.product|{}|*)}}
         */
        setProduct(state, action) {
            let payload = action.payload;
            return {...state, product: payload.product,attachments:payload.attachments,}
        },

        /**
         * 设置Hscode
         * @param state
         * @param action
         * @returns {{hscode: *}}
         */
        setHscode(state, action) {
            let payload = action.payload;
            return {...state, hscode: payload.hscode};
        },

    },
    effects: {


        //----------------------------产品


        /**
         * 获取产品信息列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getProductApprovalList({payload}, { call, put, select }) {
            let page = yield select(state => state.productApproval.productPage);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            const res = yield call(getProductApprovalList, {
                productName: R.isNil(payload) || R.isNil(payload.productName) ? "" : payload.productName,
                status: R.isNil(payload) || R.isNil(payload.status) ? '2' : payload.status,
                page });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setProductList',
                    payload: {
                        productList: R.isNil(content.list) ? [] : content.list,
                        productPage : R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 根据id查询产品信息
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getProductById({ payload }, { call, put, select }) {
            const res = yield call(getProductById, {id: payload.id});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                let attachments =data.attachments;
                yield put({
                    type: "setProduct",
                    payload: {
                        product: data,
                        attachments:attachments,
                    }
                });
            }
        },

        /**
         * 产品审核
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *productApproval({ payload }, { call, put, select }) {
            const res = yield call(productApproval, payload);

            if (res.data.resultCode === "ok") {
                message.info("审核成功！");

                if(payload.onSuccess)payload.onSuccess();
            
            }else {
                if(payload.onFail)payload.onFail();
                message.error(res.data.errMessage);
            }
        },

        /**
         * 查询商品列表
         * @param call
         * @param put
         * @param select
         */
        *getHscodeByCode({payload}, { call, put, select }) {

            const res = yield call(getHscodeByCode, {hscode: payload.hscode  });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                let hscode ={};
                if(!R.isNil(content) && !R.isEmpty(content)){
                    hscode = content;

                }
                yield put({
                    type: 'setHscode',
                    payload: {
                        hscode: hscode,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

    },
    subscriptions: {
        setup({ history, dispatch }) {

        }
    }
};